package net.wds.study.student.controller;

import net.wds.study.student.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by sidongliang on 2020/9/7.
 */
@Controller
@RequestMapping(value="/")
public class LoginController {

    @Autowired
    private StudentService studentService;

    /**
     * 跳转到登录界面
     * @return
     */
    @RequestMapping(value = "/toLogin",method={RequestMethod.GET,RequestMethod.POST})
    private String toLogin(){
        return "login";
    }

    /**
     * 跳转到登录界面
     * @return
     */
    @RequestMapping(value = "/toIndex",method={RequestMethod.GET,RequestMethod.POST})
    private String toIndex(){
        return "index";
    }

    /**
     * 登录 成功进入主页、失败返回登录界面
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/login",method={RequestMethod.POST})
    private ModelAndView login(HttpServletRequest request, HttpServletResponse response){
        ModelAndView mv = new ModelAndView();
        String no = request.getParameter("no");
        String pwd = request.getParameter("password");
        if(studentService.login(no,pwd)){
            mv.setViewName("redirect:toIndex");
        }else{
            mv.addObject("no",no);
            mv.addObject("message","用户名或密码错误");
            mv.setViewName("redirect:toLogin");
        }
        return mv;
    }

}
